﻿using System;

using Storyteller.Framework.Math;

namespace Storyteller.Framework.Graphics.Particles.Emitters
{
	public class CircleEmitter : ParticleEmitter
	{
		public float Radius { get; set; }
		public bool Edge { get; set; }
		public bool Radiate { get; set; }

		public CircleEmitter(int maximumParticles, int particlesPerSecond, Texture texture)
			: base(maximumParticles, particlesPerSecond, texture)
		{
			Radius = 1;
		}

		public CircleEmitter(int maximumParticles, int particlesPerSecond, Texture texture, TextureRegion textureRegion)
			: base(maximumParticles, particlesPerSecond, texture, textureRegion)
		{
			Radius = 1;
		}

		protected override void EmitParticles(int count, Particle[] destination, int offset)
		{
			Vector2 position = WorldPosition + LocalPosition;

			for (int i = 0; i < count; i++)
			{
				Vector2 angle = random.NextUnitVector2();
				Particle p = destination[offset + i];

				float radiusMultiplier = this.Edge ? this.Radius : this.Radius * random.NextFloat();

				p.Position = position + new Vector2(angle.X * radiusMultiplier, angle.Y * radiusMultiplier);
				p.Velocity = (Radiate ? angle : random.NextUnitVector2()) * InitialSpeed;
				p.Lifetime = 0;
				p.Scale = InitialScale;
				p.Rotation = InitialRotation;
				p.Color = InitialColor;
			}
		}
	}
}
